import argparse
import sys
import os

def parse_args():
    parser = argparse.ArgumentParser("")
    parser.add_argument("--tsv", type=str,required=True)
    parser.add_argument("--write_path", type=str,required=True)
    return parser.parse_args()


def inspect(input,output):
    outlier=0
    outlier_deepsignal=0
    outlier_dorado=0
    outlier_bs=0
    outlier_reads=set()
    mapq=[]
    with open(input,'r') as rb:
        for line in rb:
            words=line.strip().split('\t')
            readid=words[0]
            seq=words[3]
            pred_pos=[int(x)for x in words[5].split(',')]
            pred_dorado=[float(x) for x in words[6].split(',')]
            pred_deepsignal=[float(x) for x in words[7].split(',')]
            #pred_mean=words[8].split(',')
            bis_label=[int(float(x)) for x in words[15].split(',')]
            for i in range(len(pred_pos)):
                pos=pred_pos[i]
                if pos+2<len(seq):
                    if seq[pos:pos+2]!='CG':
                        outlier+=1
                        if pred_dorado[i]!=-1:
                            outlier_dorado+=1
                        if pred_deepsignal[i]!=-1:
                            outlier_deepsignal+=1
                        if bis_label[i]!=-1:
                            outlier_bs+=1
                        outlier_reads.add(readid)
            mapq.append(int(words[10]))
    print('outlier sum: {}'.format(outlier))
    print('outlier_deepsignal: {}'.format(outlier_deepsignal))
    print('outlier_dorado: {}'.format(outlier_dorado))
    print('outlier_bs: {}'.format(outlier_bs))
    print('outlier_reads: {}'.format(len(outlier_reads)))
    print('average mapq: {}'.format(sum(mapq)/len(mapq)))
    with open(output, 'w') as output_file:
        for readid in outlier_reads:
            output_file.write(readid+'\n')

if __name__ == '__main__':
    args=parse_args()
    inspect(args.tsv,args.write_path)

        